package com.scu.bank.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scu.bank.model.dao.ResearchProjectDao;
import com.scu.bank.model.example.ResearchProjectExample;
import com.scu.bank.model.pojo.AcademicActivitie;
import com.scu.bank.model.pojo.OrgInfo;
import com.scu.bank.model.pojo.ResearchProject;
import com.scu.bank.model.base.BaseServiceImpl;
import com.scu.bank.model.pojo.UserInfo;
import com.scu.bank.service.OrgInfoService;
import com.scu.bank.service.ResearchProjectService;
import com.scu.bank.service.UserInfoService;
import com.scu.bank.util.ListUtils;
import com.scu.bank.util.StringGenerateUtil;
import com.scu.bank.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by pmeng on 2021/01/05.
 */
@Service
@Transactional
public class ResearchProjectServiceImpl extends BaseServiceImpl<ResearchProject,ResearchProjectDao> implements ResearchProjectService {
    @Autowired
    private ResearchProjectDao researchProjectDao;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private OrgInfoService orgInfoService;

    @Override
    public ResearchProjectDao getDao() {
        return researchProjectDao;
    }

    @Override
    public PageInfo<ResearchProject> list(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        ResearchProjectExample example = new ResearchProjectExample();
        example.createCriteria().andIsDelEqualTo(false);
        PageInfo pageInfo = new PageInfo(researchProjectDao.selectByExample(example));

        List<ResearchProject> list = pageInfo.getList();
        if (!ListUtils.isEmpty(list)){
            list.forEach((temp)->{
                UserInfo userInfo = userInfoService.getById(temp.getOwnerId());
                temp.setOwnerName(userInfo == null ? "" : userInfo.getUserName());
                // 目前项目存放的orgid
                OrgInfo orgInfo = orgInfoService.getById(temp.getOrgCode());
                temp.setOrgName(orgInfo == null ? "不存在的组织机构" : orgInfo.getOrgName());
            });
        }
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public void addResearchProject(ResearchProject researchProject) {
        researchProject.setStatus("0");
        researchProject.setOwnerId(WebUtil.getCurrentUserId());
        String proNo = StringGenerateUtil.generatePrimaryKey().substring(2);
        researchProject.setProNo(proNo);
        save(researchProject);
    }

    @Override
    public List<Map<String, String>> getProject() {
        List<Map<String, String>> projects = new ArrayList<>();
        ResearchProjectExample example = new ResearchProjectExample();
        example.createCriteria()
                .andIsDelEqualTo(false);

        example.setOrderByClause("create_time desc");

        List<ResearchProject> researchProjects = researchProjectDao.selectByExample(example);

        for (ResearchProject researchProject : researchProjects){
            Map<String,String> pro = new HashMap<>();
            pro.put("id",researchProject.getId());
            pro.put("label",researchProject.getProName());
            projects.add(pro);
        }

        return projects;
    }

    @Override
    public List<String> getParentOrgIds(String orgId) {
        List<String> res = new ArrayList<>();
        OrgInfo orgInfo = orgInfoService.getById(orgId);
        if (orgInfo == null){
            return res;
        }

        String orgCode = orgInfo.getOrgCode();
        res.add(0,orgInfo.getId());
        while (true){
            if (orgCode.length()<4){
                break;
            }
            orgCode = orgCode.substring(0, orgCode.length() - 4);
            // 根据id查询
            orgInfo = orgInfoService.getByOrgCode(orgCode);
            res.add(0,orgInfo.getId());
        }
        return res;
    }
}
